package cn.wolfcode.crm.service.imp;

import cn.wolfcode.crm.domain.Salary;
import cn.wolfcode.crm.mapper.SalaryMapper;
import cn.wolfcode.crm.qo.PageResult;
import cn.wolfcode.crm.qo.SalaryQueryObject;
import cn.wolfcode.crm.service.ISalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class SalaryServiceImpl implements ISalaryService {
    @Autowired
    private SalaryMapper salaryMapper;


    @Override
    public void deleteById(Long id) {
        salaryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Salary record) {
        salaryMapper.insert(record);
    }

    @Override
    public Salary selectById(Long id) {
        return salaryMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateById(Salary salary) {
        salaryMapper.updateByPrimaryKey(salary);
    }

    @Override
    public PageResult query(SalaryQueryObject qo) {
        Date date = qo.getDate();
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(5, 1);
            Date first = calendar.getTime();
            calendar.add(2, 1);
            Date last = calendar.getTime();
            qo.setBigenDate(first);
            qo.setEndDate(last);
        }
        int count = salaryMapper.rows(qo);
        if (count == 0)
            return new PageResult(count, Collections.EMPTY_LIST);
        else {
            return new PageResult(count, salaryMapper.queryForList(qo));
        }
    }

    @Override
    public List<Salary> selectAll() {
        return salaryMapper.selectAll();
    }

    @Override
    public Salary selectByJopNumber(String jopNumber) {
        return salaryMapper.selectByJopNumber(jopNumber);
    }
}
